#pragma once

#include<iostream>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

const int num = 5;

typedef void*(*rb_t)(void*);

typedef enum
{
  NEW = 0,
  RUNNING,
  EXIT
}Thread_Status;

class Thread
{
public:
  Thread(int num,rb_t func,void* args)
    :_thread(0)
    ,_func(func)
    ,_args(args)
    ,_status(NEW)
  {
    char buffer[24];
    snprintf(buffer,sizeof(buffer),"thread->%d",num);
    _name = buffer;
  }
 
  int status()
  {
    return _status;
  }

  string name()
  {
    return _name;
  }

  pthread_t thread()
  {
    if(_status == RUNNING)
    {
      return _thread;
    }
    else
    {
      cout << "The thread is not running" << endl;
      return 0;
    }
  }

  void run()
  {
    int n = pthread_create(&_thread,nullptr,_func,_args);
    if(n != 0)
    {
      cout << "Thread create occur error : " << strerror(n) << endl;
      exit(1);
    }
  }

  void join()
  {
    int n = pthread_join(_thread,nullptr);
    if(n != 0)
    {
      cout << "Thread create occur error : " << strerror(n) << endl;
      exit(2);
    }
  }

  ~Thread()
  {}
private:
  pthread_t _thread;
  string _name;
  rb_t _func;
  void* _args;
  Thread_Status _status;
};
